package com.qf.student.dao.impl;

import com.qf.student.dao.IStudentDao;
import com.qf.student.entity.Student;
import com.qf.student.util.DBUtils;

import java.util.Date;
import java.util.List;

/**
 * @version1.0
 * @auth dmh
 */
public class StudentDaoImpl implements IStudentDao {
    /**
     * 底层增加学生数据的方法
     * @param student
     * @return
     */
    @Override
    public int insert(Student student) {
        String sql = "insert into t_student(name,sex,age,email,phone,create_time,loves,course) values(?,?,?,?,?,?,?,?)";
        // 1.准备一个Buffer，把多个爱好用逗号隔开的方式写到buffer里面
        StringBuffer stringBuffer=new StringBuffer();
        List<String> strings= student.getLoves();
        for (String s : strings) {
            stringBuffer.append(s).append(",");
        }
        return DBUtils.write(sql, student.getName(), student.getSex(), student.getAge(), student.getEmail(), student.getPhone(), new Date(),stringBuffer.toString(),student.getCourse());
    }

    /**
     * 底层通过查找id来修改学生数据的方法
     * @param student
     * @return
     */
    @Override
    public int updateById(Student student) {
        String sql = "update t_student set name = ?,sex = ?,age = ?,email = ?,phone = ? where id = ?";
        return DBUtils.write(sql, student.getName(), student.getSex(), student.getAge(), student.getEmail(), student.getPhone(), student.getId());
    }

    /**
     * 底层通过id来查询指定学生数据的方法
     * @param id
     * @return
     */
    @Override
    public Student selectById(Integer id) {
        String sql = "select * from t_student where id = ?";
        return DBUtils.readOne(sql, Student.class, id);
    }

    /**
     * 底层根据id来删除学生数据的方法
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        String sql = "delete from t_student where id = ?";
        return DBUtils.write(sql, id);
    }

    /**
     * 底层查询学生列表的方法
     * @return
     */
    @Override
    public List<Student> selectList() {
        String sql = "select * from t_student";
        return DBUtils.readList(sql, Student.class);
    }

    @Override
    public Student selectByName(String name) {
        String sql = "select * from t_student where name = ?";
        return DBUtils.readOne(sql, Student.class, name);
    }
}
